package com.igroupes.ovineherd.model.attribute.address;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.igroupes.ovineherd.dto.response.Meta;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.LocationStyle;
import com.igroupes.ovineherd.model.attribute.BasicAttribute;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.enums.MySqlTypeKey;
import com.igroupes.ovineherd.model.utils.ListUtils;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import com.igroupes.ovineherd.util.SpringPropertyUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * 位置
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class LocationModelData extends BasicAttribute{

    public static final Integer MAX_NUMBER = 10;
    public static final Integer MIN_NUMBER = 0;
    public static final String SIZE_LABEL = "可填写最大个数";
    private static Integer SQL_LENGTH = 50;
//    private NumberStyle size;

    public LocationModelData() {
        super();
//        this.size = new NumberStyle(SIZE_LABEL, MIN_NUMBER, true,MIN_NUMBER, MAX_NUMBER);
    }

    public Boolean verifyField( LocationModelData locationModelData) {
        super.verifyField(locationModelData);
//        new NumberStyle().verify(locationModelData.getSize());
        return true;
    }
    public Boolean verifyFieldV2( LocationModelData basic) {
        super.verifyFieldV2(basic);
//        new NumberStyle().verify(new NumberStyle(SIZE_LABEL, basic.getSize().getValue(), true,MIN_NUMBER, MAX_NUMBER));
        return true;
    }
    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        LocationModelData locationModelData = (LocationModelData) parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData, dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        fieldQuery.setLength(SQL_LENGTH);
        String fieldName = fieldQuery.getFieldName();
        DataModelAttributeEntity dataModelAttributeEntity = fieldQuery.getDataModelAttributeEntity();
        JSONObject jsonObject = dataModelAttributeEntity.getJson();
//        jsonObject.set("size", Convert.toInt(locationModelData.getSize().getValue()));
        jsonObject.set("dbName", fieldName);
        dataModelAttributeEntity.setJson(jsonObject);
        dataModelAttributeEntity.setDbNumber(2);
        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
        List<FieldQuery> fieldQueries1 = new ArrayList<>();

        fieldQuery.setFieldName(fieldName);
        fieldQuery.setComment("");
        fieldQueries1.add(fieldQuery);
//        FieldQuery fieldQuery2 = new FieldQuery();
//        BeanUtil.copyProperties(fieldQuery, fieldQuery2);
//        fieldQuery2.setFieldName(fieldName+"2");
//        fieldQuery2.setComment("");
//        fieldQuery.setDataModelAttributeEntity(null);
//        fieldQueries1.add(fieldQuery2);
        return fieldQueries1;
    }

    @Override
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        JSONObject jsonObject = super.getAttribute(modelDataJsonAttributeQuery);
//        jsonObject.set("size", new NumberStyle(SIZE_LABEL, modelDataJsonAttributeQuery.getJson().get("size"), true,MIN_NUMBER, MAX_NUMBER));
        return jsonObject;
    }
    @Override
    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        super.verifyRequestData(modelAttributeEntity, value);
        List<FieldApiResponse> fieldDescribes = new ArrayList<>();
        if(value == null){
            return fieldDescribes;
        }
        JSONObject jsonObject = modelAttributeEntity.getJson();
        JSONObject object = JSONUtil.parseObj(value);

//        String reg="^(\\-|\\+)?\\d+(\\.\\d+)?$";
//        if(!value.toString().matches(reg)){
//            throw new ModelException(modelAttributeEntity.getName() + "传值格式不正确");
//        }
        if(modelAttributeEntity.getIsNull().equals(BooleanKey.FALSE.getCode())){
            if(StringUtils.isBlank(value.toString())){
                throw new ModelException(modelAttributeEntity.getName()+"不能为空");
            }
        }
        if(ObjectUtil.isNotNull(value)){
            FieldApiResponse fieldApiResponse = new FieldApiResponse();
            fieldApiResponse.setDbName(modelAttributeEntity.getDbName()+"1");
            fieldApiResponse.setValue(ObjectUtil.toString(object));
            fieldApiResponse.setIsStr(true);
            fieldApiResponse.setType(MySqlTypeKey.VARCHAR.getCode());
            fieldDescribes.add(fieldApiResponse);
        }
//        if(!StringUtils.isBlank(object.get("lng").toString())){
//        }
//        if(!StringUtils.isBlank(object.get("lat").toString())){
//            FieldApiResponse fieldApiResponse = new FieldApiResponse();
//            fieldApiResponse.setDbName(modelAttributeEntity.getDbName()+"2");
//            fieldApiResponse.setValue(object.get("lat"));
//            fieldApiResponse.setIsStr(true);
//            fieldApiResponse.setType(MySqlTypeKey.VARCHAR.getCode());
//            fieldDescribes.add(fieldApiResponse);
//        }
        return fieldDescribes;
    }

    @Override
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery dataModelAttributeEntity) {

        return new JSONObject(new LocationStyle(
                dataModelAttributeEntity.getName(),"",
                dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode()), SpringPropertyUtils.baiduKey()
        ), false);
    }

    @Override
    public Meta getMeta(ModelDataJsonAttributeQuery dataModelAttributeEntity) {
        return null;
    }

    @Override
    public Object getData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        if(ObjectUtil.isNotNull(value)){
            return JSONUtil.parseObj(value.toString());
        }
        return value;
    }
}
